//
//  YYSDetailNewsVideoController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/19.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSDetailNewsVideoController.h"
#import "YYSNewsVideoCell.h"
#import "YYSPlayVideoController.h"
#import "YYSHomeListModel.h"
@interface YYSDetailNewsVideoController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic , strong) NSMutableArray <Video *>*models;

@end

@implementation YYSDetailNewsVideoController
static NSString *ID = @"YYSNewsVideoCell";
-(NSMutableArray<Video *> *)models
{
    if (_models == nil) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    //    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, -170);
    [self setUI];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"id"] = @(self.ID);
    dict[@"page"] = @1;
    [NSObject POST:VIDEO_LIST_URL parameters:dict progress:^(NSProgress *downloadProgress) {
        
    } completionHandler:^(id responseObj, NSError *error) {
        NSLog(@"%@",responseObj);
        if ([responseObj[@"errno"] integerValue] == 0) {
            [self.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_URL_TEST,responseObj[@"result"][@"cover"]]]];
            self.nameLabel.text = responseObj[@"result"][@"name"];
            for (NSDictionary *dict in responseObj[@"result"][@"video"]) {
                
                Video *model = [Video modelWithJSON:dict];
                model.ID = dict[@"id"];
                [self.models addObject:model];
            }
            [self.collectionView reloadData];
        }
    }];
}
- (void)setUI
{
    
    if ([self.title isEqualToString:@"新闻资讯"]) {
        _nameLabel.text = @"新闻小站";
        _iconImage.image = [UIImage imageNamed:@"新闻资讯banner"];
    }else if ([self.title isEqualToString:@"新鲜视频"]){
        _nameLabel.text = @"拍客小站";
        _iconImage.image = [UIImage imageNamed:@"休闲搞笑banner"];
    }else if ([self.title isEqualToString:@"热门电影"]) {
        _nameLabel.text = @"热门电影";
        _iconImage.image = [UIImage imageNamed:@"国产电影banner"];
    }else if ([self.title isEqualToString:@"国产电影"]) {
        _nameLabel.text = @"国产电影";
        _iconImage.image = [UIImage imageNamed:@"国产电影banner"];
    }else if ([self.title isEqualToString:@"海外电影"]) {
        _nameLabel.text = @"海外电影";
        _iconImage.image = [UIImage imageNamed:@"海外电影banner"];
    }else if ([self.title isEqualToString:@"重磅推荐"]) {
        _nameLabel.text = @"重磅推荐";
        _iconImage.image = [UIImage imageNamed:@"娱乐圈banner"];
    }else if ([self.title isEqualToString:@"国内综艺"]) {
        _nameLabel.text = @"国内综艺";
        _iconImage.image = [UIImage imageNamed:@"国内综艺banner"];
    }else if ([self.title isEqualToString:@"海外综艺"]) {
        _nameLabel.text = @"海外综艺";
        _iconImage.image = [UIImage imageNamed:@"海外综艺banner"];
    }else if ([self.title isEqualToString:@"国语热歌"]) {
        _nameLabel.text = @"国语热歌";
        _iconImage.image = [UIImage imageNamed:@"国语热歌banner"];
    }else if ([self.title isEqualToString:@"藏语经典"]) {
        _nameLabel.text = @"藏语经典";
        _iconImage.image = [UIImage imageNamed:@"藏语歌曲banner"];
    }else if ([self.title isEqualToString:@"海外劲歌"]) {
        _nameLabel.text = @"海外劲歌";
        _iconImage.image = [UIImage imageNamed:@"海外热歌banner"];
    }else if ([self.title isEqualToString:@"自制影片"]) {
        _nameLabel.text = @"自制影片";
        _iconImage.image = [UIImage imageNamed:@"自制电影banner"];
    }else if ([self.title isEqualToString:@"猜你喜欢"]) {
        _nameLabel.text = @"猜你喜欢";
        _iconImage.image = [UIImage imageNamed:@"自拍视频banner"];
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    label.text = self.title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    if ([self.title isEqualToString:@"国语热歌"]) {
        layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width)/3  , 160);
    } else if ([self.title isEqualToString:@"藏语经典"]){
        layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width)/3  , 160);
    } else if ([self.title isEqualToString:@"海外劲歌"]){
        layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width)/3  , 160);
    } else {
        layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width)/2  , 160);
    }
    
    
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;
    //    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    // 注册
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YYSNewsVideoCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}
#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.models.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YYSNewsVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.video = self.models[indexPath.row];
    cell.type = RecommandType;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YYSPlayVideoController *play = [[YYSPlayVideoController alloc] init];
    [self.navigationController pushViewController:play animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
